import face_recognition
import cv2  #openCV库
import numpy as np  #矩阵算法库
#原址：https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture_cnn.py
#这是一个从你的网络摄像头实时视频运行人脸识别的演示。这比
#另一个例子，但它包括一些基本的性能调整，使事情运行得更快：
# 1。以1/4分辨率处理每个视频帧（尽管仍以全分辨率显示）
# 2。只检测其他视频帧中的人脸。
#请注意：此示例要求只安装opencv（cv2库）以从网络摄像机中读取。
#使用人脸识别库时，不需要使用opencv。只有当你想运行这个时才需要它
#具体演示。如果您在安装它时遇到问题，请尝试其他不需要它的演示。

#获取对网络摄像机的引用0（默认值）
video_capture = cv2.VideoCapture(0)

# 加载示例图片并学习如何识别它。
zhoujielun_image = face_recognition.load_image_file("D:/picture/face/gujiahua.jpg")
zhoujielun_face_encoding = face_recognition.face_encodings(zhoujielun_image)[0]

# # 加载第二个示例图片并学习如何识别它。
# huangjunkai_image = face_recognition.load_image_file("D:/picture/huangjunkai.jpg")
# huangjunkai_face_encoding = face_recognition.face_encodings(huangjunkai_image)[0]
#
# me_image = face_recognition.load_image_file("D:/picture/me.jpg")
# me_face_encoding = face_recognition.face_encodings(me_image)[0]
# me1_image = face_recognition.load_image_file("D:/picture/me1.jpg")
# me1_face_encoding = face_recognition.face_encodings(me1_image)[0]
# chenluliang_image = face_recognition.load_image_file("D:/picture/chenluliang.jpg")
# chenluliang_face_encoding = face_recognition.face_encodings(chenluliang_image)[0]
# chenweiliang_image = face_recognition.load_image_file("D:/picture/chenweiliang.jpg")
# chenweiliang_face_encoding = face_recognition.face_encodings(chenweiliang_image)[0]
# gujiahua_image = face_recognition.load_image_file("D:/picture/gujiahua.jpg")
# gujiahua_face_encoding = face_recognition.face_encodings(gujiahua_image)[0]

# 创建已知人脸编码及其名称的数组
known_face_encodings = [
    zhoujielun_face_encoding,
    # huangjunkai_face_encoding,
    # me_face_encoding,
    # me1_face_encoding,
    # chenluliang_face_encoding,
    # chenweiliang_face_encoding,
    # gujiahua_face_encoding
]
known_face_names = [
    "lihongwu1",
    # "HuangJunKai",
    # "Me",
    # "ChenLuQiang",
    # "ChenLuLiang",
    # "ChenWeiLiang",
    # "GuJiaHua"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # 抓取一帧视频
    ret, frame = video_capture.read()

    # 将视频帧的大小调整为1/4，以便更快地进行人脸识别处理
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # 将图像从bgr颜色（opencv使用）转换为rgb颜色（人脸识别使用）
    rgb_small_frame = small_frame[:, :, ::-1]

    # 只处理其他每帧视频以节省时间
    if process_this_frame:
        # 查找当前视频帧中的所有面和面编码
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # 查看人脸是否与已知人脸匹配
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # # 如果在已知的面部编码中发现匹配，只需使用第一个。
            # if True in matches:
            #     first_match_index = matches.index(True)
            #     name = known_face_names[first_match_index]

            # 或者，使用与新面距离最小的已知面
            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = known_face_names[best_match_index]
            face_names.append(name)

    process_this_frame = not process_this_frame

    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # 自我们检测到的帧被缩放到1/4大小后，将备份面位置缩放
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # 在脸上画一个方框
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # 在面下绘制一个名称为的标签
        # cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 0), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (0,0, 0),1)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # 点击键盘上的“Q”退出！
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()